import gtk,gobject

COL_NAME = 1

class treeview_helper:
    def __init__(self,treeview):
        try:
            if self.already == True:pass
        except:
            self.already = True
            self.treeview = treeview
            # Set up one column
            column = gtk.TreeViewColumn("Name"
                    , gtk.CellRendererText(),
                    text=COL_NAME) # position of column
            self.treeview.append_column(column)

        # Create a model for the data
        self.treestore = gtk.TreeStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)


        self.folders = {}

        fullcats = ["Game Screens","Game Objects","Workstates","Sprites"]


        # Create our object categories
        for cat in fullcats:
            self.folders[cat] = treeview_folder(self.treestore,cat)


        # Tell the treeview we want to display this data
        self.treeview.set_model(self.treestore)

    def clear(self):
        self.__init__(self.treeview)



    def init_item_list(self,widget=None):
        pass

    def update_item_list(self,widget=None):
        # Inefficient - should be updated better
        self.__init__(self.treeview)
        self.populate_from_game(self.game)
        pass

    def populate_from_game(self,game):
        self.game = game
        a = game.workstates.keys()
        a.sort()
        for sg in a:
                sel_iter = self.folders["Workstates"].insert_iter
                newworkstate = treeview_item(self.treestore,sg,sel_iter,game.workstates[sg])

        a = game.gamescreens.keys()
        a.sort()
        for screen in a:
                sel_iter = self.folders["Game Screens"].insert_iter
                newworkstate = treeview_item(self.treestore,screen,sel_iter,game.gamescreens[screen])

        a = game.gameobjects.keys()
        a.sort()
        for obj in a:
                sel_iter = self.folders["Game Objects"].insert_iter
                newworkstate = treeview_item(self.treestore,obj,sel_iter,game.gameobjects[obj])

        a = game.sprites.keys()
        a.sort()
        for spr in a:
                sel_iter = self.folders["Sprites"].insert_iter
                newworkstate = treeview_item(self.treestore,spr,sel_iter,game.sprites[spr])

class treeview_folder:
    def __init__(self,treestore,name):
        
        self.name = name
        self.insert_iter = treestore.append(None, [None,name])

    def handle(self):
        pass

class treeview_item:
    def __init__(self,treestore,name,parent,obj):
        
        self.name = name
        insert_iter = treestore.append(parent, [obj,name])
        
        
